'==================================================================================================
'	Author		David Segura
'	URL			http://winpeguy.com/
'	Source		http://winpe.codeplex.com/
'	Version		20130910
'==================================================================================================
'	Purpose:
'	(Unreleased) Driver Tools Section
'==================================================================================================
'	File Locations in Windows:
'	%DeployRoot%\Boot\+WinPE\Menu\Scripts\DriverTools.vbs
'==================================================================================================
'	File Location in WinPE:
'	X:\Menu\Scripts\DriverTools.vbs
'==================================================================================================
'	Configuration:
'	None.  Editing this file is not recommended
'==================================================================================================
'	Change Log:
'	20130910	Codeplex Beta
'==================================================================================================
Dim strUSBIndex, MakeUSBFAT32

Sub subDriversWinPE
	If objFSO.FileExists(strSystemDrive & "\Windows\System32\dpinst.exe") Then
		sCmd = "cmd /c devcon rescan"
		objShell.Run sCmd, 2, True
		sCmd = "cmd /c devcon dp_enum > X:\devcon_dp_enum.txt"
		objShell.Run sCmd, 2, False
		sCmd = "cmd /c devcon hwids * > X:\devcon_hwids.txt"
		objShell.Run sCmd, 2, False
		sCmd = "cmd /c devcon classes > X:\devcon_classes.txt"
		objShell.Run sCmd, 2, False
		sCmd = "cmd /c devcon status * > X:\devcon_status.txt"
		objShell.Run sCmd, 2, False
		sCmd = "cmd /c devcon findall =hdc > X:\devcon_findall_hdc.txt"
		objShell.Run sCmd, 2, False
		sCmd = "cmd /c devcon findall =net > X:\devcon_findall_net.txt"
		objShell.Run sCmd, 2, False
		sCmd = "cmd /c devcon hwids =hdc > X:\devcon_hwids_hdc.txt"
		objShell.Run sCmd, 2, False
		sCmd = "cmd /c devcon hwids =net > X:\devcon_hwids_net.txt"
		objShell.Run sCmd, 2, False
		sCmd = "cmd /c devcon listclass hdc > X:\devcon_listclass_hdc.txt"
		objShell.Run sCmd, 2, False
		sCmd = "cmd /c devcon listclass net > X:\devcon_listclass_net.txt"
		objShell.Run sCmd, 2, False
		sCmd = "cmd /c devcon driverfiles * > X:\devcon_driverfiles.txt"
		objShell.Run sCmd, 2, False

		
		If strPEBootType = "DRIVE" Then
			If objFSO.FolderExists(strPEBootDrive & "+Drivers\WinPE") Then
				MsgBox "If Drivers need to be added to the Boot Image, submit an Incident with the following information:" & vbCrLf & "" & vbCrLf & _
				"Incident Area: Software.BHIMAGE.Incident.Issue with Boot Disc" & vbCrLf & _
				"Group: L2_IOS_CCS_Packaging Deployment Services" & vbCrLf & "" & vbCrLf & _
				"Include a URL where to download the drivers, keeping in mind, only Windows 8 Drivers will be added",64,MsgBoxTitleShort & "Install WinPE Drivers"
				
				MsgBox "Drivers will be loaded from " & strPEBootDrive & "+Drivers\WinPE" & vbCrLf & "" & vbCrLf & _
				"To manually install drivers, use the following command as a reference:" & vbCrLf & _
				"dpinst /PATH F:\path\to\drivers" & vbCrLf & "" & vbCrLf & _
				"Press OK to load the drivers into WinPE",64,MsgBoxTitleShort & "Install WinPE Drivers"
				sCmd = "dpinst /PATH " & strPEBootDrive & "+Drivers\WinPE"
				objShell.Run sCmd, 1, True
				sCmd = "devcon rescan"
				objShell.Run sCmd, 2, True
				MsgBox "Install WinPE Drivers" & vbCrLf & "" & vbCrLf & _
				"Drivers should now be loaded in WinPE",64,MsgBoxTitleShort & "Install WinPE Drivers"
			End If
		Else
			MsgBox "Install WinPE Drivers" & vbCrLf & "" & vbCrLf & _
			"This utility can only be run when booting from a USB Drive",16,MsgBoxTitleShort & "Install WinPE Drivers"
		End If

	Else
		MsgBox "Cannot find " & strSystemDrive & "\Windows\System32\dpinst.exe" & vbCrLf & "" & vbCrLf & _
		"This utility is required to load drivers in WinPE",16,MsgBoxTitleShort & "Install WinPE Drivers"
	End If
End Sub

Sub subDriversWindows
	MsgBox "This feature is not available in this release" & vbCrLf & "" & vbCrLf & _
	"",64,MsgBoxTitleShort & "Install Windows Drivers Offline"
End Sub
'==================================================================================================
Sub subUSBCreate
	strUSBIndex = ""
	strUSBDriveFound = "NONE"
	intUSBDriveFound = "NONE"
	intMakeBootUSB = "NONE"
	MakeUSB = "NONE"
	MakeUSBFAT32 = "NONE"
	
	MsgBox "Do not remove your Boot Drive or CD-ROM" & vbCrLf & "" & vbCrLf & _
	"This will be used to copy files to the new USB Drive",64,MsgBoxTitleShort & "Create USB Boot"
	
	Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

	Set colDisks = objWMIService.ExecQuery _
		("Select * from Win32_DiskDrive WHERE InterfaceType='USB'")

	For Each objDisk in colDisks
		strUSBInterfaceType = objDisk.InterfaceType
		strUSBCaption = objDisk.Caption
		strUSBIndex = objDisk.Index
		strUSBSize = Round(objDisk.Size / 1000 / 1000 / 1000,0)
		strUSBDriveFound = "YES"
		strUSBDeviceID = objDisk.DeviceID
		intUSBDriveFound = 	MsgBox ("Found USB Drive:" & vbCrLf & "" & vbCrLf & _
							strUSBSize & " GB " & strUSBCaption & " on Disk Index " & strUSBIndex & vbCrLf & "" & vbCrLf & _
							"Is this the correct USB Drive to format and make bootable?",36,MsgBoxTitleShort & "Create USB Boot")
		
		If intUSBDriveFound = vbYes Then
			strUSBDriveFound = "YES"
			Exit For
		Else
			strUSBDriveFound = "NO"
		End If
	Next

	
	If strUSBDriveFound = "YES" Then
		intMakeBootUSB = 	Msgbox("USB Drive " & strUSBSize & " GB " & strUSBCaption & " on Disk Index " & strUSBIndex & " will be formatted" & vbCrLf & _
							"" & vbCrLf & _
							"Do you want to continue and format this drive?",36,MsgBoxTitleShort & "Create USB Boot")
		
		If intMakeBootUSB = vbYes Then
			MakeUSB = "YES"
		Else
			MakeUSB = "NO"
		End If
	Else
		MsgBox "Could not find a USB Drive to use" & vbCrLf & "" & vbCrLf & _
		"",64,MsgBoxTitleShort & "Create USB Boot"
	End If
	


	If MakeUSB = "YES" and strUSBSize <= 32 Then
		'We can make this a UEFI
		intMakeUSBType =	Msgbox("This drive can be formatted NTFS or FAT32 for UEFI Support" & vbCrLf & _
							"" & vbCrLf & _
							"Press Yes for NTFS or press No for FAT32",36,MsgBoxTitleShort & "Create USB Boot")

		If intMakeUSBType = vbYes Then
		Else
			MakeUSBFAT32 = "YES"
		End If
	End If

	If MakeUSB = "YES" Then
		subDiskpartUSB
	End If

End Sub
'==================================================================================================
Sub subUSBUpdate
	MsgBox "Updating a USB from WinPE is not available" & vbCrLf & "" & vbCrLf & _
	"Run \\YourServer\BHIMAGE\Boot\WinPE USB Update.vbs on your imaging share" & vbCrLf & "" & vbCrLf & _
	"",64,MsgBoxTitleShort & "Update USB Boot"
End Sub
'==================================================================================================
Sub subDiskpartUSB
	If objFSO.FileExists(envTemp & "\diskpartUSB.txt") Then
		objFSO.DeleteFile(envTemp & "\diskpartUSB.txt")
	End If
	
	Set objTextStream = objFSO.OpenTextFile(envTemp & "\diskpartUSB.txt", 2, True)
	objTextStream.WriteLine "LIST DISK"
	objTextStream.WriteLine "SELECT DISK " & strUSBIndex
	objTextStream.WriteLine "CLEAN"
	objTextStream.WriteLine "CREATE PARTITION PRIMARY"
	If MakeUSBFAT32 = "YES" Then
		objTextStream.WriteLine "FORMAT FS=FAT32 QUICK LABEL=" & """WINPEGUY"""
	Else
		objTextStream.WriteLine "FORMAT FS=NTFS QUICK LABEL=" & """WINPEGUY"""
	End If
	objTextStream.WriteLine "ASSIGN"
	objTextStream.WriteLine "ACTIVE"
	objTextStream.Close

	If objFSO.FileExists(envTemp & "\diskpartUSB.txt") Then
		sCmd = "diskpart /s " & envTemp & "\diskpartUSB.txt"
		objShell.Run sCmd, 1, True
	End If

	MsgBox "USB Drive format is complete" & vbCrLf & "" & vbCrLf & _
	"Press OK to Copy the content from the original Boot Device on drive " & strPEBootDrive,64,MsgBoxTitleShort & "Create USB Boot"

	strComputer = "."
	Set objWMIService = GetObject("winmgmts:" _
		& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
	Set colDrives = objWMIService.ExecQuery _
		("Select * from Win32_LogicalDisk where VolumeName = 'WINPEGUY'")
	For Each objDrive in colDrives
		strDriveLetter = objDrive.Caption
		If MakeUSBFAT32 = "YES" Then
			objDrive.VolumeName = "WinPE UEFI"
			objDrive.Put_
		Else
			objDrive.VolumeName = "WinPE NTFS"
			objDrive.Put_
		End If
	Next

	If objFSO.FileExists(strPEBootDrive & "bootmgr") Then
		'MsgBox "Copying Files from " & strPEBootDrive & " to " & strDriveLetter & "\"
		'objFSO.CopyFolder strPEBootDrive & "*\*" , strDriveLetter & "\" , OverwriteExisting
		sCmd = "robocopy " & strPEBootDrive & " " & strDriveLetter & "\ *.* /E /NDL /XJ"
		objShell.Run sCmd, 1, True
	Else
		MsgBox "Could not locate files on " & strPEBootDrive & " to copy",16,MsgBoxTitleShort & "Create USB Boot"
	End If

	MsgBox "Create Boot USB is now complete" & vbCrLf & "" & vbCrLf & _
	"",64,MsgBoxTitleShort & "Create USB Boot"
End Sub

